package com.hwg.emp.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.hwg.emp.common.dto.KeyQueryDto;
import com.hwg.emp.common.dto.PaginationResult;
import com.hwg.emp.common.exception.EmpException;
import com.hwg.emp.common.service.CommonService;
import com.hwg.emp.common.util.DBUtil;
import com.hwg.emp.dao.DaoFactory;
import com.hwg.emp.dao.DeptDAO;
import com.hwg.emp.dao.UserDAO;
import com.hwg.emp.model.DeptMD;
import com.hwg.emp.model.UserMD;
import com.hwg.emp.service.DeptService;

public class DeptServiceImpl extends CommonService implements DeptService {

	private DeptDAO dao = DaoFactory.getDao(DeptDAO.class);
	private UserDAO userDao = DaoFactory.getDao(UserDAO.class);
	
	@Override
	public DeptMD getDeptById(String id) throws Exception {
		DeptMD dept = dao.select("id", id);
		//断言不为null，如果为空则抛出异常信息为"部门不存在"
		assertNotNull("部门不存在", dept);
		return dept;
	}

	@Override
	public boolean addDept(DeptMD dept) throws Exception {
		boolean flag = false;
		$("部门名不能为空", dept.getName());
		assertFalse("部门名已存在", dao.exist("name", dept.getName()));
		if(dept.getManager().getAccount()!=null&&dept.getManager().getAccount().trim().length()!=0){
			assertTrue("部门经理不存在", userDao.exist("account", dept.getManager().getAccount()));
			UserMD user = userDao.select("account", dept.getManager().getAccount());
			dept.setManager(user);
		}
		//开启事务
		DBUtil.beginTranscation();
		int total = dao.insert(dept);
		if(total>0){
			if(dept.getManager().getAccount()!=null&&dept.getManager().getAccount().trim().length()!=0){
				dept.setMembers(0);
				dao.updateMembersPlusOne(dept);
			}
			//事务提交
			DBUtil.commitTranscation();
			flag = true;
		}else{
			//事务回滚
			DBUtil.rollbackTranscation();
		}
		return flag;
	}

	@Override
	public boolean deleteDeptByIds(String... ids) throws Exception { 
		DBUtil.beginTranscation();
		int count = 0;
		for (String id : ids) {
			if(dao.select("id", id).getMembers()>0){
				throw new EmpException("部门下还有员工，不能被删除！");
			}else{
				count += dao.delete("id", id);
			}
		}
		if(count==ids.length){
			DBUtil.commitTranscation();
			return true;
		}else{
			DBUtil.rollbackTranscation();
			return false;
		}
	}

	@Override
	public boolean modifyDept(DeptMD dept) throws Exception {
		boolean f = dao.exist("name", dept.getName());
		if(f){
			throw new EmpException("部门已存在");
		}
		DBUtil.beginTranscation();
		boolean flag = false;
		try{
			dao.updateName(dept);
			flag=true;
		}catch(Exception e){
			throw new EmpException("修改失败");
		}
		return flag;
	}

	@Override
	public PaginationResult<DeptMD> getDeptsByKey(KeyQueryDto dto) throws Exception {
		String key = ifBlank(dto.getKey(), null);
		int pageSize = ifNull(dto.getPageSize(), 10);
		int pageNo = ifNull(dto.getPageNo(), 1);
		dto.setKey(key);
		dto.setPageNo(pageNo);
		dto.setPageSize(pageSize);
		List<DeptMD> pageData = new ArrayList<DeptMD>(pageSize);
		//总条数
		int totalRows = dao.selectByKey(dto, pageData);
		//组装查询结果对象
		PaginationResult<DeptMD> result = new PaginationResult<DeptMD>();
		result.setPageNo(pageNo);
		result.setPageSize(pageSize);
		result.setRows(pageData);
		result.setTotalRecords(totalRows);
		return result;
	}

	@Override
	public DeptMD selectOneDept(String name) throws Exception {
		return dao.selectOneDept(name);
	}

	@Override
	public Boolean existByName(String name) throws Exception {
		return dao.exist("name", name);
	}
}
